package com.hmall.utils;

import java.util.Objects;

public class UserThreadLocal {

    // 定义一个 ThreadLocal 变量
    private static final ThreadLocal<String> threadLocal = new ThreadLocal<>();

    // 获取 ThreadLocal 变量的值
    public static String get() {
        return threadLocal.get();
    }

    // 设置 ThreadLocal 变量的值
    public static void set(String value) {
        threadLocal.set(value);
    }

    // 移除 ThreadLocal 变量的值
    public static void remove() {
        threadLocal.remove();
    }

    // 检查 ThreadLocal 变量是否有值
    public static boolean hasValue() {
        return Objects.nonNull(threadLocal.get());
    }

    // 获取或设置默认值
    public static String getOrDefault(String defaultValue) {
        String value = threadLocal.get();
        if (value == null) {
            threadLocal.set(defaultValue);
            return defaultValue;
        }
        return value;
    }



}